angular.module('Controller')
.controller('QgyCorporateEditCtrl',($scope,urls,qgyApiService,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    $scope.type=type=items.type
    $scope.data=data=items.data
    $scope.modalTitle=if type=='new' then '新建企业资讯' else '编辑企业资讯'
    $scope.mnfReadFlagList=commonConstant.notificationReadFlagList()
    $scope.mnfBuzTypeList=commonConstant.notificationBuzTypeList()
    $scope.mnfBuzTypeListConsole=_.filter($scope.mnfBuzTypeList,(theItem)->theItem.value in  [1,2,5])
    $scope.mnfTypeList=commonConstant.notificationTypeList()
    $scope.mnfTypeListConsole=_.filter($scope.mnfTypeList,(theItem)->theItem.value in  [1,3])
    if data.expiredTime
        data.expiredTime=new Date(expiredTime)
    $scope.modalApartmentList=[]
    loadApartment=()->
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.modalApartmentList=$scope.modalApartmentList.concat(resp.message.content)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    newData=()->
        reqData={
            "type": data.type
            "buzType": data.buzType
            "subject": data.subject
            "content": data.content
            "userType": 1,      #接收消息用户类型，1-管理员，2-租户 必传
            "receiverId": data.receiverId
#            "buzObject": null,  #业务对象
            "buzLink": data.buzLink
            "expiredTime": if data.expiredTime then moment(data.expiredTime).format('YYYY-MM-DD') else undefined
        }
        $http.post("/notification",reqData)
        .success((resp)->
            if resp.code==0
                wfAlert.toastInfo($scope.modalTitle+'成功')
                $uibModalInstance.close('done')
            else
                wfAlert.toastError(resp.message)
        )
    editData=()->
        $uibModalInstance.close('done')
        ''
    $scope.pickType=()->
        if $scope.data.type==1
            $scope.data.receiverId=0
        else if $scope.data.type==3
            $scope.data.receiverId=$scope.modalApartmentList[0].id
        else
            $scope.data.receiverId=undefined
    $scope.confirm = ()->
        if type=='new'
            newData()
        else
            editData()
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
    $scope.showUserInfo=()->
        if $scope.userInfo==undefined
            return '未获得用户'
        else
            if $scope.userInfo.name
                return $scope.userInfo.name
            else
                return '会员'+$scope.userInfo.id
    $scope.fetchUser=()->
        if data.phone?.length==11
            pm=qgyApiService.fetchAsyncUser(data.phone)
            pm.then((res)->
                if res
                    $scope.userInfo=res
                    data.receiverId=res.id
                else
                    $scope.userInfo=undefined
            ,(err)->
                $scope.userInfo=undefined
            )
        else
            $scope.userInfo=undefined
)